#include "serialportwideget.h"
#include "ui_serialportwideget.h"
#include <QThread>


QList<int>
    frequency_right_pressure,frequency_left_pressure,
    nine_pressure_data_right,nine_pressure_data_list_original_location_right,nine_pressure_data_temp_right,
    nine_pressure_data_left,nine_pressure_data_list_original_location_left,nine_pressure_data_temp_left;

serialPortWideget::serialPortWideget(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::serialPortWideget)
{
    ui->setupUi(this);
    Qt::WindowFlags flags = Qt::Dialog;
    flags |= Qt::WindowCloseButtonHint;
    this->setWindowFlags(flags);

    searchedPort = new runThread;
    searchedPort->start();
    connect(searchedPort,SIGNAL(emitPortList(QStringList)),this,SLOT(getPortList(QStringList)));
    connect(ui->BluetoothPortBox,&QComboBox::currentTextChanged,this,[=](QString name){constPortName = name;});//Box发生选中事件时，转到savePortName槽函数
    connect(ui->BluetoothPortBoxLeft,&QComboBox::currentTextChanged,this,[=](QString name){constPortNameLeft = name;});

    ui->stop->setEnabled(false);



    connect(ui->begin, &QPushButton::clicked, this,[=](){
        if(constPortName==constPortNameLeft&&!constPortName.isEmpty()){
            QMessageBox::critical(this,"串口错误","所选串口重复");
        }else if(constPortName.isEmpty()||constPortNameLeft.isEmpty()){
            QMessageBox::critical(this,"串口错误","所选串口为空");
        }else{
            this->hide();
        }
    });

     connect(ui->stop, &QPushButton::clicked, this,[=](){
         this->hide();
    });
}


serialPortWideget::~serialPortWideget()
{
if(t->isRunning()){
    t->quit();
    t->wait();
    delete t;
    delete mythreadOne;
        }

if(tTwo->isRunning()){
    tTwo->quit();
    tTwo->wait();
    delete tTwo;
    delete mythreadTwo;
}
delete searchedPort;
delete ui;
}

void serialPortWideget::setDetectionTime(double time)
{
    detectionTime = time;
}


void serialPortWideget::getPortList(QStringList PortList){


    ui->BluetoothPortBox->clear();
    ui->BluetoothPortBoxLeft->clear();
    ui->BluetoothPortBox->addItems(PortList);
    ui->BluetoothPortBoxLeft->addItems(PortList);
    qDebug()<<"可用串口列表:"<<PortList;


}

void serialPortWideget::threadSetting(){
    /*创建一个mythreadOne多线程，将QThread线程的数据采集操作放在mythreadOne线程里运行*/
    ui->BluetoothPortBox->setEnabled(false);
    ui->begin->setEnabled(false);
    ui->stop->setEnabled(true);
    t = new QThread;
    mythreadOne = new MyThread;
    mythreadOne->moveToThread(t);
    t->start();
//    connect(t,SIGNAL(finished()),t,SLOT(deleteLater()));
//    connect(t,SIGNAL(finished()),mythreadOne,SLOT(deleteLater()));
    connect(mythreadOne,&MyThread::emitCsvFileClose,this,[=](){
        emit emitCloseRight();
        disconnect(mythreadOne,0,0,0);
        t->quit();
        t->wait();
        mythreadOne->deleteLater();
        t->deleteLater();
    });

    connect(t,&QThread::finished,this,[=](){
        qDebug()<<"--------------------------------↑蓝牙数据采集结束Right↑--------------------------------";
    });
    connect(this,&serialPortWideget::emitPortInfo,mythreadOne,&MyThread::devPortInfo);

    emit emitPortInfo(constPortName,detectionTime);

    qDebug()<<"--------------------------------↓蓝牙数据采集开始Right↓--------------------------------";
    connect(mythreadOne,&MyThread::emitSendData,this,[=](QList<int> data,double time){
//                qDebug()<<data;

        if(data.size()==9){
            frequency_right_pressure = data;
        }
        data.clear();
    });



    connect(mythreadOne,&MyThread::emitHeatMap,this,[=](QList<int> nine_pressure_data,QList<int> nine_pressure_data_list_original_location,QList<int> nine_pressure_data_temp){
       nine_pressure_data_right = nine_pressure_data;
       nine_pressure_data_list_original_location_right = nine_pressure_data_list_original_location;
       nine_pressure_data_temp_right = nine_pressure_data_temp;

       nine_pressure_data.clear();
       nine_pressure_data_list_original_location.clear();
       nine_pressure_data_temp.clear();
    });


}

void serialPortWideget::leftThreadSetting(){
    /*创建一个mythreadOne多线程，将QThread线程的数据采集操作放在mythreadOne线程里运行*/

    ui->BluetoothPortBoxLeft->setEnabled(false);
    ui->begin->setEnabled(false);
    ui->stop->setEnabled(true);

    tTwo = new QThread;

    mythreadTwo = new MyThread;
    mythreadTwo->moveToThread(tTwo);
    tTwo->start();
//    connect(tTwo,SIGNAL(finished()),tTwo,SLOT(deleteLater()));
//    connect(tTwo,SIGNAL(finished()),mythreadTwo,SLOT(deleteLater()));
    connect(mythreadTwo,&MyThread::emitCsvFileClose,this,[=](){
        emit emitCloseLeft();
        disconnect(mythreadTwo,0,0,0);
        tTwo->quit();
        tTwo->wait();
        mythreadTwo->deleteLater();
        tTwo->deleteLater();

    });
    connect(tTwo,&QThread::finished,this,[=](){
        qDebug()<<"--------------------------------↑蓝牙数据采集结束Left↑--------------------------------";
    });
    connect(this,&serialPortWideget::emitPortLeftInfo,mythreadTwo,&MyThread::devPortInfo);

    emit emitPortLeftInfo(constPortNameLeft,detectionTime);
    qDebug()<<"--------------------------------↓蓝牙数据采集开始Left↓--------------------------------";
    connect(mythreadTwo,&MyThread::emitSendData,this,[=](QList<int> data,double time){
//                qDebug()<<"data:"<<data;
        if(data.size()==9){
            frequency_left_pressure = data;
        }
       data.clear();
    });

    connect(mythreadTwo,&MyThread::emitHeatMap,this,[=](QList<int> nine_pressure_data,QList<int> nine_pressure_data_list_original_location,QList<int> nine_pressure_data_temp){
        nine_pressure_data_left = nine_pressure_data;
        nine_pressure_data_list_original_location_left = nine_pressure_data_list_original_location;
        nine_pressure_data_temp_left = nine_pressure_data_temp;

       nine_pressure_data.clear();
       nine_pressure_data_list_original_location.clear();
       nine_pressure_data_temp.clear();
    });

}



